package com.jhgsys.internal.base.controller;

import com.jhgsys.internal.base.entity.BaseProjectArea;
import com.jhgsys.internal.base.service.IBaseProjectAreaService;
import com.jhgsys.internal.common.annotation.Log;
import com.jhgsys.internal.common.controller.BaseController;
import com.jhgsys.internal.common.entity.FebsConstant;
import com.jhgsys.internal.common.entity.FebsResponse;
import com.jhgsys.internal.common.entity.QueryRequest;
import com.jhgsys.internal.common.exception.FebsException;
import com.jhgsys.internal.common.utils.FebsUtil;
import com.wuwenze.poi.ExcelKit;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 项目区域 Controller
 *
 * @author admin
 * @date 2024-07-29 15:56:11
 */
@Slf4j
@Validated
@Controller
public class BaseProjectAreaController extends BaseController {

    @Autowired
    private IBaseProjectAreaService baseProjectAreaService;

    @GetMapping(FebsConstant.VIEW_PREFIX + "baseProjectArea")
    @RequiresPermissions("baseProjectArea:view")
    public String baseProjectAreaIndex(){
        return FebsUtil.view("base/baseProjectArea/baseProjectAreaList");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "baseProjectArea/add")
    @RequiresPermissions("baseProjectArea:add")
    public String baseProjectAreaAdd(){
        return FebsUtil.view("base/baseProjectArea/baseProjectAreaEdit");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "baseProjectArea/update/{id}")
    @RequiresPermissions("baseProjectArea:update")
    public String baseProjectAreaUpdate(Model model, @PathVariable("id") String id){
        model.addAttribute("baseProjectArea", baseProjectAreaService.findBaseProjectAreaById(id));
        return FebsUtil.view("base/baseProjectArea/baseProjectAreaEdit");
    }

    /**
     * 查询（所有）
     * @author admin
     * @date 2024-07-29 15:56:11
     */
    @GetMapping("baseProjectArea")
    @ResponseBody
    @RequiresPermissions(value = {"baseProjectArea:view","baseProject:view"},logical = Logical.OR)
    public FebsResponse getAllBaseProjectAreas(BaseProjectArea baseProjectArea) {
        return new FebsResponse().success().data(baseProjectAreaService.findBaseProjectAreas(baseProjectArea));
    }

    /**
     * 查询（分页）
     * @author admin
     * @date 2024-07-29 15:56:11
     */
    @GetMapping("baseProjectArea/list")
    @ResponseBody
    @RequiresPermissions("baseProjectArea:view")
    public FebsResponse baseProjectAreaList(QueryRequest request, BaseProjectArea baseProjectArea) {
        Map<String, Object> dataTable = getDataTable(this.baseProjectAreaService.findBaseProjectAreas(request, baseProjectArea));
        return new FebsResponse().success().data(dataTable);
    }

    /**
     * 新增 BaseProjectArea
     * @author admin
     * @date 2024-07-29 15:56:11
     */
    @Log("新增BaseProjectArea")
    @PostMapping("baseProjectArea")
    @ResponseBody
    @RequiresPermissions("baseProjectArea:add")
    public FebsResponse addBaseProjectArea(@Valid BaseProjectArea baseProjectArea) throws FebsException {
        try {
            baseProjectArea.setCreatedBy(getCurrentUser().getUserName());
            this.baseProjectAreaService.createBaseProjectArea(baseProjectArea);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "新增BaseProjectArea失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }

    /**
     * 逻辑删除 BaseProjectArea
     * @author admin
     * @date 2024-07-29 15:56:11
     */
    @Log("逻辑删除BaseProjectArea")
    @GetMapping("baseProjectArea/delete/{id}")
    @ResponseBody
    @RequiresPermissions("baseProjectArea:delete")
    public FebsResponse deleteBaseProjectArea(@PathVariable("id") String baseProjectAreaIds) throws FebsException {
        try {
            BaseProjectArea baseProjectArea = new BaseProjectArea();
            baseProjectArea.setDeletedBy(getCurrentUser().getUserName());
            baseProjectArea.setDeletedTime(new Date());
            baseProjectArea.setDeletedFlg(FebsConstant.INVALID);
            this.baseProjectAreaService.deleteBaseProjectArea(baseProjectArea,baseProjectAreaIds);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "删除BaseProjectArea失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }

    /**
    * 物理删除 BaseProjectArea
    * @author admin
    * @date 2024-07-29 15:56:11
    */
    @Log("物理删除BaseProjectArea")
    @GetMapping("baseProjectArea/physicallyDelete/{id}")
    @ResponseBody
    @RequiresPermissions("baseProjectArea:update")
    public FebsResponse physicallyDeleteBaseProjectArea(@PathVariable("id") String baseProjectAreaIds) throws FebsException {
        try {
            this.baseProjectAreaService.physicallyDeleteBaseProjectArea(baseProjectAreaIds);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "删除BaseProjectArea失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }

    /**
     * 修改 BaseProjectArea
     * @author admin
     * @date 2024-07-29 15:56:11
     */
    @Log("修改BaseProjectArea")
    @PostMapping("baseProjectArea/update")
    @ResponseBody
    @RequiresPermissions("baseProjectArea:update")
    public FebsResponse updateBaseProjectArea(BaseProjectArea baseProjectArea) throws FebsException {
        try {
            baseProjectArea.setUpdatedBy(getCurrentUser().getUserName());
            this.baseProjectAreaService.updateBaseProjectArea(baseProjectArea);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "修改BaseProjectArea失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }

    /**
     * 导出 BaseProjectArea的excel
     * @author admin
     * @date 2024-07-29 15:56:11
     */
    @PostMapping("baseProjectArea/excel")
    @ResponseBody
    @RequiresPermissions("baseProjectArea:view")
    public void export(QueryRequest queryRequest, BaseProjectArea baseProjectArea, HttpServletResponse response) throws FebsException {
        try {
            List<BaseProjectArea> baseProjectAreas = this.baseProjectAreaService.findBaseProjectAreas(queryRequest, baseProjectArea).getRecords();
            ExcelKit.$Export(BaseProjectArea.class, response).downXlsx(baseProjectAreas, false);
        } catch (Exception e) {
            String message = "导出Excel失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }
}
